#include<iostream>
#include<unistd.h>
#include<vector>
#include"Thread.h"

#define TICKETS 10000
#define CUNSUMERS 10
pthread_mutex_t _mutex=PTHREAD_MUTEX_INITIALIZER;

void Buy(size_t& tickets)
{
    while(tickets>0)
    {
        pthread_mutex_lock(&_mutex);
        std::cout<<"thread "<<pthread_self()<<" get ticket: "<<tickets<<std::endl;
        --tickets;
        pthread_mutex_unlock(&_mutex);
        usleep(100);
    }
}

int main()
{
    size_t tickets=TICKETS;
    size_t consumers=CUNSUMERS;
    std::vector<Thread<size_t>> threads;
    threads.reserve(consumers);
    while(consumers--)
    {
        threads.emplace_back(Thread<size_t>(Buy,tickets,"thread"+std::to_string(consumers)));
        threads.back().Start();
    }
    for(auto &e:threads)
    {
        e.Wait();
    }
    return 0;
}